﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 会告
    /// </summary>
    public class WilltellDAL
    {
        public WilltellDAL(HoteContext hoteContext)
        {
            HoteContext = hoteContext;
        }

        public HoteContext HoteContext { get; }
        /// <summary>
        /// 添加会告
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int WillteAdd(WilltellModel info)
        {
            try
            {
                int res = HoteContext.willtellModels.Count(o => o.WiteTitle == info.WiteTitle);
                if (res > 0)
                {
                    return -1;//门店名称重复
                }
                HoteContext.willtellModels.Add(info);
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int willtellDelete(int id)
        {
            try
            {
                var res = HoteContext.willtellModels.Find(id);
                if (res != null)
                {
                    res.IsDeleted = true;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int willtellDeleteAll(string ids)
        {
            try
            {
                var list = ids.Split(',');
                foreach (var id in list)
                {
                    var info = HoteContext.willtellModels.Find(Convert.ToInt32(id));
                    info.IsDeleted = true;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int willtellUpdateAll(string ids, int state)
        {
            try
            {
                var idsAll = ids.Split(",");
                foreach (var id in idsAll)
                {
                    var info = HoteContext.willtellModels.Find(Convert.ToInt32(id));
                    info.State = state;
                }
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WilltellModel GetwillteId(int id)
        {
            try
            {
                return HoteContext.willtellModels.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改会告信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int willteUpdate(WilltellModel info)
        {
            try
            {
                var res = HoteContext.willtellModels.Count(o => o.IsDeleted == false && o.WiteTitle == info.WiteTitle && o.id != info.id);
                if (res > 0)
                {
                    return -1;
                }
                HoteContext.willtellModels.Update(info);
                return HoteContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="WiteTitle"></param>
        /// <param name="WiteType"></param>
        /// <returns></returns>
        public List<WilltellModel> GetShopList(out int totalCount, out int totalPage, int pageindex = 1, int pageSize = 2, string? WiteTitle = "", int? WiteType = null)
        {
            try
            {
                var list = HoteContext.willtellModels.Where(o => o.IsDeleted == false).ToList();
                if (!string.IsNullOrEmpty(WiteTitle))
                {
                    list = list.Where(o => o.WiteTitle.Contains(WiteTitle)).ToList();
                }
                if (WiteType != null)
                {
                    list = list.Where(o => o.WiteType == WiteType).ToList();
                }
                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = list.OrderByDescending(o => o.id).Skip((pageindex - 1) * pageSize).Take(pageSize).ToList();
                return list;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
